package com.itlong.cloud.property;

import com.itlong.cloud.POJO.DTO.common.PadFaceSyncDataDTO;
import com.itlong.cloud.equip.property.IPadFaceSyncDataServiceClient;
import com.itlong.cloud.project.property.IPropertyValueServerServiceClient;
import com.itlong.cloud.utils.date.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Map;
import java.util.Set;

/**
 * <desc>
 *      过期临时通行任务调度类
 * </desc>
 *
 * @createDate 2019/05/29
 */
@EnableScheduling // 启用定时任务
@RestController
public class PropertyTemporaryOverdueTimeTask {
    private static Logger LOG = LoggerFactory.getLogger(PropertyTemporaryOverdueTimeTask.class);

    @Autowired
    IPropertyValueServerServiceClient iPropertyValueServerServiceClient;

    @Autowired
    private IPadFaceSyncDataServiceClient iPadFaceSyncDataServiceClient;

    /**
     * <desc>
     *      过期临时通行结果表
     * </desc>
     *
     * @author Qiang.S
     * @creatDate 2019/05/29
     */
    @Scheduled(cron = "0 * * * * ?") // 每1分钟执行一次
//    @Scheduled(cron = "0/5 * * * * ?") // 每30秒执行一次
    public void timeTaskSychro() {
        try {
            Map<String, Set<String>> mapSet = iPropertyValueServerServiceClient.doTemporaryOverdue();
//            LOG.info("【定时任务】过期临时通行结果表执行成功，当前时间：" + DateUtil.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));
            mapSet.forEach((key, value) -> {
                try {
                    StringBuilder roomIds = new StringBuilder();
                    for (String roomId : value) {
                        if (roomIds.length() == 0) {
                            roomIds.append(roomId);
                        } else {
                            roomIds.append(",").append(roomId);
                        }
                    }
                    PadFaceSyncDataDTO padFaceSyncDataDTO = new PadFaceSyncDataDTO();
                    padFaceSyncDataDTO.setOperateType("0");
                    padFaceSyncDataDTO.setRoomId(roomIds.toString());
                    padFaceSyncDataDTO.setProjectId(key);
                    iPadFaceSyncDataServiceClient.userOrFloorSync(padFaceSyncDataDTO);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }catch (Exception e){
            LOG.error("【定时任务】过期临时通行结果表执行失败，当前时间：" + DateUtil.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss:"),e);
        }
    }
}
